import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:live_music/components/feedback/PageFeedBack.dart';
import 'package:live_music/components/index/videoCard.dart';
import 'package:live_music/components/ui/theme.dart';
import 'package:live_music/pages/home/message/message_page/atAndCommentInfo_index.dart';
import 'package:live_music/pages/home/message/message_page/atAndCommentInfo_list.dart';
import 'package:live_music/pages/home/message/message_page/follow_index_page.dart';
import 'package:live_music/pages/home/message/message_page/notification_index_page.dart';
import 'package:live_music/utils/colors.dart';

class MessageIndexPage extends StatefulWidget {
  MessageIndexPage({Key key}) : super(key: key);

  @override
  _MessageIndexPageState createState() => _MessageIndexPageState();
}

class _MessageIndexPageState extends State<MessageIndexPage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  // 下拉刷新控制器
  EasyRefreshController _easyRefreshController;
  // 独立的滚动视图 防止tab切换时触发下拉刷新
  ScrollController _scrollController;
  // 列表数组模型
  // List<SingerListItem> _singerList = SingerList([]).list;
  int page = 1;
  int limit = 10;
  bool hasMore = true;
  bool loading = true;
  bool error = false;
  String errorMsg;

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
    _scrollController = ScrollController();
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    _scrollController.dispose();
    super.dispose();
  }

  // 刷新回调(null为不开启刷新)
  Future _onRefresh() async {
    if (error) {
      setState(() => error = false);
    }
    page = 1;
    print('asd');
    // await _getSingers();
    // 恢复刷新状态 使onLoad再次可用
    _easyRefreshController.resetLoadState();
  }

  // 加载回调(null为不开启加载)
  Future _onLoad() async {
    await Future.delayed(const Duration(seconds: 2), () {
      setState(() {
        // 控制器关闭加载功能，还可以设置没有更多数据noMore，可以根据需求自己变更，这里同样也需要在数据请求成功进行关闭。
        _easyRefreshController.finishLoad(success: true, noMore: false);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorRes.themeBg,
        appBar: AppBar(
          backgroundColor: Colors.white,
          automaticallyImplyLeading: false,
          title: Text(
            '消息',
            style: TextStyle(color: Colors.black, fontSize: 16),
          ),
          centerTitle: true,
        ),
        body: Column(children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                  child: InkWell(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 10, bottom: 10),
                      child: Image.asset(
                        'assets/images/icons/' + "message_tongzhi.png",
                        width: 50,
                        height: 50,
                      ),
                    ),
                    Text('通知',
                        style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w500))
                  ],
                ),
                onTap: () => {Get.to(NotificationIndexPage())},
              )),
              Expanded(
                  child: InkWell(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 10, bottom: 10),
                      child: Image.asset(
                        'assets/images/icons/' + "message_shoucang.png",
                        width: 50,
                        height: 50,
                      ),
                    ),
                    Text('新增关注',
                        style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w500))
                  ],
                ),
                onTap: () => {Get.to(FollowIndexPage())},
              )),
              Expanded(
                  child: InkWell(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 10, bottom: 10),
                      child: Image.asset(
                        'assets/images/icons/' + "message_pinglun.png",
                        width: 50,
                        height: 50,
                      ),
                    ),
                    Text('评论和@',
                        style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w500))
                  ],
                ),
                onTap: () => {Get.to(AtAndCommentInfoIndexPage(1))},
              )),
            ],
          ),
          Container(
              margin: EdgeInsets.only(top: 10, bottom: 10),
              height: MediaQuery.of(context).size.height - 261,
              child: EasyRefresh(
                  firstRefresh: true,
                  firstRefreshWidget: PageFeedBack(firstRefresh: true).build(),
                  emptyWidget: PageFeedBack(
                    loading: loading,
                    error: error,
                    empty: false,
                    errorMsg: errorMsg,
                    onErrorTap: () => _easyRefreshController.callRefresh(),
                    onEmptyTap: () => _easyRefreshController.callRefresh(),
                  ).build(),
                  header: ClassicalHeader(),
                  footer: ClassicalFooter(),
                  controller: _easyRefreshController,
                  enableControlFinishRefresh: false,
                  enableControlFinishLoad: true,
                  onRefresh: _onRefresh,
                  onLoad: _onLoad,
                  child: ListView.builder(
                      controller: _scrollController,
                      itemCount: 0,
                      itemBuilder: (BuildContext context, int index) {
                        return Container(
                          padding: const EdgeInsets.symmetric(horizontal: 16),
                          margin: const EdgeInsets.only(top: 16),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              CircleAvatar(
                                radius: 25,
                                backgroundImage: NetworkImage(
                                    'https://img2.baidu.com/it/u=2650069889,3641570332&fm=253&fmt=auto&app=138&f=JPEG?w=752&h=500'),
                              ),
                              Expanded(
                                child: Container(
                                  padding: const EdgeInsets.only(bottom: 15),
                                  margin: const EdgeInsets.only(left: 24),
                                  // decoration: BoxDecoration(
                                  //   border: Border(
                                  //     bottom: BorderSide(
                                  //       width: .5,
                                  //       color: WcaoTheme.placeholder.withOpacity(.5),
                                  //     ),
                                  //   ),
                                  // ),
                                  child: Column(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text(
                                            '云儿',
                                            style: TextStyle(
                                              fontSize: 14,
                                              fontWeight: FontWeight.bold,
                                            ),
                                          ),
                                          Text(
                                            "昨天",
                                            style: TextStyle(
                                                fontSize: WcaoTheme.fsSm,
                                                color: WcaoTheme.secondary),
                                          )
                                        ],
                                      ),
                                      Container(
                                        margin: const EdgeInsets.only(top: 4),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Expanded(
                                              child: Container(
                                                padding: const EdgeInsets.only(
                                                    right: 24),
                                                child: Text(
                                                  '你好呀',
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                  style: TextStyle(
                                                    color: WcaoTheme.secondary,
                                                    fontSize: 12,
                                                  ),
                                                ),
                                              ),
                                            )
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              )
                            ],
                          ),
                        );
                      })))
        ]));
  }

  @override
  bool get wantKeepAlive => true;
}
